/* 
* Copyright (C) 2011-2018 ShenZhen iBOXSaaS Information Technology Co.,Ltd. 
* 
* All right reserved. 
* 
* This software is the confidential and proprietary 
* information of iBOXSaaS Company of China. 
* ("Confidential Information"). You shall not disclose 
* such Confidential Information and shall use it only 
* in accordance with the terms of the contract agreement 
* you entered into with iBOXSaaS inc. 
* 
*/

package org.iboxpay.open.common.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.iboxpay.open.common.constants.LogEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import com.iboxpay.common.json.MapperUtils;
import com.iboxpay.common.utils.IpAddressUtil;

/**
 * 
 * LogUtils.java
 * @author xuxiao
 * @date 2018/06/21
 */
public class LogUtils {
    
    private static final Logger interfaceLog = LoggerFactory.getLogger(LogEnums.interfaceLog.getCode());

    public static void fillIp(Map<String, String> accessMap, HttpServletRequest request, String transactionId) {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            interfaceLog.warn("[{}] [HTTP] [request] - Exception:", transactionId, e);
        } catch (Exception e2) {
            interfaceLog.warn("[{}] [HTTP] [request] - Exception:", transactionId, e2);
        }
        
        String remoteIp = IpAddressUtil.checkIpAddr(request);
        String localIp = addr == null ? "" : String.format("%s:%s", addr.getHostAddress(), request.getServerPort());
        accessMap.put(LogEnums.remoteIp.getCode(), remoteIp);
        accessMap.put(LogEnums.localIp.getCode(), localIp);
    }
    
    public static void fillResult(Map<String, String> accessMap, Object result) {
        String transactionId = MDC.get(LogEnums.transactionId.getCode());
        if (result != null) {
            try {
                Map<String, Object> baseResponse = null;
                if (result instanceof Map) {
                    baseResponse = (Map<String, Object>) result;
                } else if (result instanceof String) {
                    baseResponse = MapperUtils.toMap((String)result);
                } else {
                    baseResponse = MapperUtils.beanToMap(result);
                }
                if (baseResponse != null) {
                    accessMap.put(LogEnums.resultCode.getCode(), baseResponse.get(LogEnums.resultCode.getCode()) == null
                        ? "" : (String)baseResponse.get(LogEnums.resultCode.getCode()));
                    accessMap.put(LogEnums.resultCode.getCode(), baseResponse.get(LogEnums.resultCode.getCode()) == null
                        ? "" : (String)baseResponse.get(LogEnums.resultCode.getCode()));
                    accessMap.put(LogEnums.errorDesc.getCode(), baseResponse.get(LogEnums.errorDesc.getCode()) == null
                        ? "" : (String)baseResponse.get(LogEnums.errorDesc.getCode()));
                }
            } catch (Exception e) {
                interfaceLog.warn("[{}] [HTTP] [request] - Exception:", transactionId, e);
            }
        }
    }
}
